﻿using LPLS.Commons.Enums;
using LPLS.DBEntities.ConfigModels.DeviceInfoModels;
using LPLS.DBInterface.ConfigModels.DeviceInfoModels;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceBaseInfoConfigModule.ViewModels
{
    public class DeviceComponentInfoViewModel : BindableBase
    {
        IDeviceComponentInfoService _deviceComponentInfoService;
        IDeviceBaseInfoService _deviceBaseInfoService;
        public DeviceComponentInfoViewModel(IDeviceComponentInfoService deviceComponentInfoService
            , IDeviceBaseInfoService deviceBaseInfoService)

        {

            _deviceComponentInfoService = deviceComponentInfoService;
            _deviceBaseInfoService = deviceBaseInfoService;
            InitData();

            AddCommand = new DelegateCommand(AddComponent);
            DeleteCommand = new DelegateCommand(DeleteComponent);
            UpdateCommand = new DelegateCommand(UpdateComponent);

            DeviceInfoConfigEvent.DeviceListChangeEvent += DeviceInfoConfigEvent_DeviceListChangeEvent;
        }

        private void DeviceInfoConfigEvent_DeviceListChangeEvent(List<DeviceBaseInfo> obj)
        {
            DeviceList.Clear();

            var devidelist = _deviceBaseInfoService.GetData();
            DeviceList.AddRange(devidelist);
        }

        private ObservableCollection<DeviceBaseInfo> _DeviceList;
        /// <summary>
        /// 设备全部数据
        /// </summary>
        public ObservableCollection<DeviceBaseInfo> DeviceList
        {
            get { return _DeviceList; }
            set {

                SetProperty(ref _DeviceList,value);
              }
        }

        private DeviceBaseInfo _SelectDeviceModel;
        public DeviceBaseInfo SelectDeviceModel
        {
            get { return _SelectDeviceModel; }
            set
            {
                SetProperty(ref _SelectDeviceModel, value);

                if (_SelectDeviceModel!=null)
                {
                    DeviceComponentList = new ObservableCollection<DeviceComponentInfo>(_SelectDeviceModel.DeviceComponents);
                    RaisePropertyChanged(nameof(DeviceComponentList));
                    this.ComponentName = string.Empty;
                    this.ComponentCode = string.Empty;
                }
              
            }
        }

        private ObservableCollection<DeviceComponentInfo> _DeviceComponentList;
        public ObservableCollection<DeviceComponentInfo> DeviceComponentList
        {
            get { return _DeviceComponentList; }
            set {
                SetProperty(ref _DeviceComponentList, value);
                
              }
        }




        private DeviceComponentInfo _GridSelectModel;

        public DeviceComponentInfo GridSelectModel
        {
            get { return _GridSelectModel; }
            set
            {
                SetProperty(ref _GridSelectModel, value);

                   if ( _GridSelectModel != null)
                {
                    this.ComponentName = GridSelectModel.ComponentName;
                    this.ComponentCode = GridSelectModel.ComponentCode;
                    this.ComboxSelectIndex = ComponentTypes.IndexOf(GridSelectModel.ComponentTypeName);
                    this.ComponentTypeName = ComponentTypes[ComboxSelectIndex];
                    RaisePropertyChanged(nameof(ComponentName));
                    RaisePropertyChanged(nameof(ComponentCode));
                    RaisePropertyChanged(nameof(ComboxSelectIndex));
                    RaisePropertyChanged(nameof(ComponentTypeName));
                }
              

            }
        }



        private string _ComponentName;

        public string ComponentName
        {
            get { return _ComponentName; }
            set {
                
                SetProperty(ref _ComponentName, value); }
        }

        private string _ComponentCode;

        public string ComponentCode
        {
            get { return _ComponentCode; }
            set { SetProperty(ref _ComponentCode, value); }
        }



        private ObservableCollection<string> _ComponentTypes;

        public ObservableCollection<string> ComponentTypes
        {
            get { return _ComponentTypes; }
            set { SetProperty(ref _ComponentTypes, value); }
        }

        private string _ComponentTypeName;

        public string ComponentTypeName
        {
            get { return _ComponentTypeName; }
            set { SetProperty(ref _ComponentTypeName, value); }
        }

        private int _ComboxSelectIndex;

        public int ComboxSelectIndex
        {
            get { return _ComboxSelectIndex; }
            set { SetProperty(ref _ComboxSelectIndex, value); }
        }

        private void InitData()
        {
            // 初始化设备列表
            LoadDeviceList();
            // 初始化组件列表
            LoadComponentList();
            // 初始化组件类型
            LoadComponentTypes();
        }
        /// <summary>
        /// 加载 部件信息
        /// </summary>
        private void LoadComponentList()
        {
            DeviceComponentList = new ObservableCollection<DeviceComponentInfo>(_deviceComponentInfoService.GetData());
        }

        /// <summary>
        /// 加载全部的设备信息
        /// </summary>
        private void LoadDeviceList()
        {


            DeviceList = new ObservableCollection<DeviceBaseInfo>(_deviceBaseInfoService.GetData());


            ;


        }

        private void LoadComponentTypes()
        {
            var desdata = DeviceComponentEnumExtension.GetAllDescriptions();
            ComponentTypes = new ObservableCollection<string>(desdata);
        }


        private string _TipMessage;

        public string TipMessage
        {
            get { return _TipMessage; }
            set { SetProperty(ref _TipMessage, value)   ; }
        }




        public DelegateCommand AddCommand { get; }

        public DelegateCommand DeleteCommand { get; }
        public DelegateCommand UpdateCommand { get; }

        private void Init() 
        {
            DeviceComponentList.Clear();
          
            InitData();
            ComponentName = string.Empty;
            ComponentCode = string.Empty;

        }

        private void AddComponent()
        {
            if (SelectDeviceModel == null)
            {
                TipMessage = "请先选择设备";
                return;

            }
            if (string.IsNullOrEmpty(ComponentName) || string.IsNullOrEmpty(ComponentCode))
            {
                TipMessage = "部件名称或部件编码不能为空";

            }
            if (ComboxSelectIndex < 0)
            {
                TipMessage = "请选择部件类型";

            }

            var model= DeviceComponentList.Where(e=>e.ComponentCode== ComponentCode&&e.DeviceBaseInfoID==SelectDeviceModel.ID)?.FirstOrDefault();
            if (model != null) { TipMessage = "该设备下的部件号重复请检查";

                return;
            }
            DeviceComponentInfo deviceComponentInfo = new DeviceComponentInfo
            {
                ComponentName = ComponentName,
                ComponentCode = ComponentCode,
                ComponentTypeName = ComponentTypes[ComboxSelectIndex],
                DeviceBaseInfoID = SelectDeviceModel.ID
               ,
                ComponentType = DeviceComponentEnumExtension.GetEnumByValue(ComboxSelectIndex)
            };
            _deviceComponentInfoService.AddData(deviceComponentInfo);
            SelectDeviceModel.DeviceComponents.Add(deviceComponentInfo);
            DeviceComponentList.Add(deviceComponentInfo);
            Init();
        }

        private void DeleteComponent()
        {
            if (GridSelectModel == null)
            {
                TipMessage = "请先选择要删除的部件";
                return;
            }
            _deviceComponentInfoService.DeleteData(GridSelectModel.ID);

            Init();
        }
        private void UpdateComponent()
        {


            if (GridSelectModel == null)
            {
                TipMessage = "请先选择要修改的部件";
                return;
            }
            if (string.IsNullOrEmpty(ComponentName) || string.IsNullOrEmpty(ComponentCode))
            {
                TipMessage = "部件名称或部件编码不能为空";
                return;
            }
            if (ComboxSelectIndex < 0)
            {
                TipMessage = "请选择部件类型";
                return;
            }

          
            GridSelectModel.ComponentName = ComponentName;
            GridSelectModel.ComponentCode = ComponentCode;
            GridSelectModel.ComponentTypeName = ComponentTypes[ComboxSelectIndex];
            GridSelectModel.ComponentType = DeviceComponentEnumExtension.GetEnumByDescription(ComponentTypes[ComboxSelectIndex]);
            _deviceComponentInfoService.UpdateData(GridSelectModel);
            Init();
        }
    }
}
